//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    imgUrl: baseJs.imgUrl,
    id: "",
    // 
    courseData: [],
    imgSrc: "",
    // 是否已经购买课程
    alreadyBuy: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      id: options.id,
    })
    this.GetCourseDetail();
    this.CheckMyIsBuyCourse();
  },

  GetCourseDetail: function () {
    var that = this;
    var id = this.data.id;
    var courseData = this.data.courseData;
    var imgSrc = this.data.imgSrc;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetCourseDetail&CourseId=" + id),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        courseData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        courseData.TeacherDescribe = courseData.TeacherDescribe.replace(/<br>/g,"");
        // console.log(courseData);
       
        that.setData({
          courseData: courseData,
        })
        wx.setNavigationBarTitle({
          title: courseData.Title
        })
      }
    })
  },
  // 是否已经购买课程
  CheckMyIsBuyCourse: function () {
    var that = this;
    var id = this.data.id;
    var uid = this.data.uid;
    var alreadyBuy = this.data.alreadyBuy;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyIsBuyCourse&CourseId=" + id + "&UID=" + uid),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data))
        console.log(json);
        if (json.ResultFlag == 1) {
          if (json.ResultMsg == '已购买') {
            that.setData({
              alreadyBuy: '1',
            })
          } else {
            that.setData({
              alreadyBuy: '0',
            })
          }
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 
  baoMingTap: function () {
    wx.showModal({
      title: '提示',
      content: '此功能暂未开放，请前往app或公众号进行查看！',
      showCancel: false
    })
  },
  // 
})